/*
 * User.java
 *
 * Created on 12 de noviembre de 2006, 01:44
 *
 * Copyright(c) 2006 FIEL (Fundaci�n de Investigaciones Econ�micas Latinoamericanas).
 * All Rights Reserved.
 * This software is the proprietary information of FIEL.
 */
package ar.org.fiel.tarifasocial.om.model;

import ar.org.fiel.tarifasocial.om.base.BaseUser;
import ar.org.fiel.tarifasocial.om.peer.UserPeer;
import java.util.List;
import org.apache.torque.util.Criteria;

/**
 * @author Ing. Luis Pablo Gallo (luispablo.gallo@gmail.com)
 */
public class User extends BaseUser implements PersistentEntity
{
    public static final String ROLE_USER = "USER";
    public static final String ROLE_ADMINISTRATOR = "ADMINISTRATOR";
    public static final String ROLE_MODERATOR = "MODERATOR";
    
    public static final String STATE_PENDING = "PENDING";
    public static final String STATE_ACTIVE = "ACTIVE";
    public static final String STATE_INACTIVE = "INACTIVE";
    
    public static final String YES = "YES";
    public static final String NO = "NO";
    
    private boolean rootUser = false;

    public void deleteUserStatistics()
    throws Exception
    {
        List<UserStatistic> userStatistics = this.getUserStatistics();
        
        for (UserStatistic userStatistic : userStatistics)
        {
            userStatistic.delete();
        }
    }
    
    public void deleteDownloads()
    throws Exception
    {
        List<Download> downloads = this.getDownloads();
        
        for (Download download : downloads)
        {
            download.delete();
        }
    }
    
    public void delete()
    throws Exception
    {
        this.deleteDownloads();
        this.deleteUserStatistics();
        
	Criteria criteria = new Criteria();
	criteria.add(UserPeer.USE_ID, this.getId());
	
	UserPeer.doDelete(criteria);
    }
    
    public String toString()
    {
	return this.getLastName() +", "+ this.getName();
    }
    
    public boolean isAdministrator()
    {
	return (this.getRoleName().equals(ROLE_ADMINISTRATOR) || this.getRoleName().equals(ROLE_MODERATOR));
    }
    
    public boolean canBeDeleted() throws Exception
    {
	return !this.isRootUser();
    }
    
    public boolean isRootUser()
    {
	return rootUser;
    }
    
    public void setRootUser(boolean rootUser)
    {
	this.rootUser = rootUser;
    }
 
    public String getMailURL()
    {
        return "mailto:" + this.getEmail();
    }
    
    public boolean equals(User user)
    {
	return !this.isNew() && user!=null && !user.isNew() &&
		user.getId().equals(this.getId());
    }
    
}